package com.example.myservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class MainActivity2 extends Activity {
    private MybindService.MyBinder myBinder;
    MyConn myconn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bind_servicelayout2);

        Log.i("life", "MainActivity2 onCreate");
        if (myconn == null) {
            Log.i("life","myconn == null");
            myconn = new MyConn(myBinder); //创建连接服务的对象
        }
        Intent intent = new Intent(MainActivity2.this, MybindService.class);
        bindService(intent, myconn, BIND_AUTO_CREATE); //绑定服务
    }

    public void navigateToPage3(View v) {
        Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
        startActivity(intent);
    }
//
//    @Override
//    protected void onStop() {
//        Log.i("life", "unbindClick1()");
//        if (myconn != null) {
//            Log.i("life","myconn != null");
//            unbindService(myconn); //解绑服务
//            myconn = null;
//        }
//        super.onStop();
//    }

    @Override
    protected void onDestroy() {
        Log.i("life", "MainActivity2 onDestroy()");
        if (myconn != null) {
            Log.i("life","myconn != null");
            unbindService(myconn); //解绑服务
            myconn = null;
        }
        super.onDestroy();
    }

    //    public void bindClick(View v) {
//        Log.i("life", "bindClick1()");
//        if (myconn == null) {
//            Log.i("life","myconn == null");
//            myconn = new MyConn(MyConn.myBinder); //创建连接服务的对象
//        }
//        Intent intent = new Intent(MainActivity2.this, MybindService.class);
//        bindService(intent, myconn, BIND_AUTO_CREATE); //绑定服务
//    }
//
//    public void callClick(View v) {
//        Log.i("life", "callClick()");
//        MyConn.myBinder.methodInBinder(); //调用服务中的方法
//    }
//
//    public void unbindClick(View v) {
//        Log.i("life", "unbindClick1()");
//        if (myconn != null) {
//            Log.i("life","myconn != null");
//            unbindService(myconn); //解绑服务
//            myconn = null;
//        }
//    }
//        private class MyConn implements ServiceConnection {//创建MyConn类,用于实现连接服务
//
//            // 当成功绑定服务时调用的方法,该方法获取MyService中的Ibinder对象
//            @Override
//            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//                myBinder = (MybindService.MyBinder) iBinder;
//                Log.i("life", "服务成功绑定, 内存地址为:" + myBinder.toString());
//            } //当服务失去连接时调用的方法
//
//            @Override
//            public void onServiceDisconnected(ComponentName componentName) {
//                // service被破坏或被杀死的时候调用
//                Log.i("life", "服务成功解绑");
//            }
//        }
}
